<template>
  <!-- 询价比详情 -->
  <div id="purchaseDetails">
    <div class="top">
      <van-nav-bar
        title="物资明细详情"
        left-arrow
        @click-left="$router.push('/purchase')"
      />
    </div>
    <div class="msg" v-show="list.length > 0">
      <div class="company">
        报价公司名称:
        {{ msg.companyName }}
      </div>
      <div class="contact">
        <div>
          报价联系人:
          {{ msg.contact }}
        </div>
        <div>
          联系电话:
          {{ msg.contactPhone }}
        </div>
      </div>
      <div >
        <div class="accessory">
          <van-uploader multiple v-model="file"> 资质附件 </van-uploader>
        </div>
      </div>
    </div>
    <div class="nothing" v-show="list.length == 0">
      <p>暂无数据</p>
    </div>
    <div class="list-item" v-for="(item, index) in list" :key="index">
      <div>
        <p class="title">{{ item.materialName }}</p>
      </div>
      <div class="row">
        <div>规格型号：{{ item.specifications }}</div>
        <div>数量：{{ item.quantityRequired }}</div>
      </div>
      <div class="row">
        <div>计量单位：{{ item.jldw }}</div>
        <div>税率：{{  (item.sl * 1) * 100 }}%</div>
      </div>
      <div class="row">
        <div>到货地：{{ item.goodsAddress }}</div>
        <div>预计交货期：{{ item.delivery }}</div>
      </div>
      <div class="rows " >
        <calendar
 
          name="wtfwtrq"
          label="实际交货期"
          placeholder="请选择日期"
          class="entrustment mandatory"
          v-model="item.jhq"
        />
      </div>
      <div>
        <div>备注：</div>
        <div>{{ item.remark }}</div>
        <!-- <textarea :class="quoteState == 0?'disableInput':''" :readonly="quoteState == 0" rows="1" style="resize:vertical;" v-model="item.remark"/> -->
      </div>
        <div>
            <!--            <div>附件：</div>-->
            <!--            <span v-for="is in item.uploadAttList" @click="upload(is.id)">{{is.filename}}</span>-->
            <div>附件：</div>
            <van-list>
                    <span v-for="is in item.uploadAttList" @click="upload(is.id)"
                          style="border: 1px black solid;display: block">
                        <van-icon name="description">{{ is.filename }}</van-icon>
                    </span>
            </van-list>
        </div>
      <div>
        <p>质量要求：</p>
        <p class="quality">{{ item.qualityRequirements }}</p>
        <div class="detail" @click="inQualityDetail(item.qualityRequirements)">
          详情
        </div>
      </div>
      <div>
        <span class="theUnitPrice mandatory">不含税单价:</span>
        <van-field
          class="inputN"
          type="number"
          placeholder="请输入金额"
          autocomplete="off"
          v-model="item.bj"
        />
        <span class="money">元</span>
      </div>
      <div>
        <!-- 总价： <input type="text" v-model="item.zj" :value="(item.bj === ''||null||undefined ? 0 :(item.bj*1) * (item.quantityRequired*1))"> -->
        总价：<span ref="totalPrice">{{
          item.bj === "" || null || undefined
            ? 0
            : item.bj * 1 * (item.quantityRequired * 1)
        }}</span>
        <!-- 总价：<span ref="totalPrice" >{{item.zj}}</span>  -->
      </div>
        <div style="color: red">
            <div>询价发布人：</div>
            <div>{{ msg.publisher }}</div>
        </div>
    </div>
   
    <div class="submit" v-show="list.length > 0">
      <div class="row TPrice" v-show="!(list.length == 0)">
      <p >不含税合计总价：{{ this.allPrice }}</p>
      <p>含税合计总价：{{ ((this.allPrice)*1.13).toFixed(2) }}</p>
    </div>
      <van-button
        round
        block
        type="info"
        @click="submit"
        :loading="loading"
        :disabled="loading"
        >提交报价</van-button
      >
    </div>
      <van-dialog v-model="isShow" show-cancel-button confirm-button-text="关闭" style="max-height: 70%!important;overflow: auto; ">
          <div v-html="htmlText"/>
      </van-dialog>
  </div>
</template>

<script>
import { Toast } from "vant";
import api from "../../http/api";
import {list} from "postcss";
export default {
  data() {
    return {
        htmlText:'',
        isShow:false,
      whether: null,
      totalPrice: "22",
      // whether: true,
      theAmountOf: "", //输入的金额
      pattern: /^(([1-9]{1}\d*)|(0{1}))((.\d{0,2})?)$/, //数字和小数点
      code: "",
      loading: false,
      quoteState:null,
      // 当前数据id
      id: null,
      // 列表数据
      noTaxPrice: "", //不含税总价
    //   taxPrice: "", //含税总价
      list: [
        // {
        //   materialName: "标题",
        //   specifications: "dsad",
        //   quantityRequired: "10",
        //   goodsAddress: "dsad",
        //   delivery: "dsad",
        //   sl:'0.13',
        //   bj: "10",
        //   remark: "dsad",
        //   jldw:'kg',
        //   qualityRequirements: "dsad",
        //   zj: "",
        // },
        // {
        //   materialName: "标题2",
        //   specifications: "asd23",
        //   quantityRequired: "10",
        //   sl:'0.13',
        //   bj: "10",
        //   jldw:'kg',
        //   goodsAddress: "dsa213d",
        //   delivery: "ds231ad",
        //   remark: "ds123ad",
        //   qualityRequirements: "ds3123ad",
        //   zj: "",
        // },
      ],
      msg: {
        // companyName: "撒大苏打",
        // contact: "啊实打实",
        // contactPhone: "18281138815",
      },
      // 资质附件
      file: [],
      fileName: [],
    };
  },
  mounted() {
    this.getDetail();
    this.quoteState = this.$route.query.quoteState
    console.log('this.quoteState :>> ', this.quoteState);
    // console.log('this.$refs.totalPrice :>> ', this.$refs.totalPrice.innerText);
  },
  computed: {
    allPrice() {
      return this.list.reduce((total, current) => {
        const price = Number(current.bj*(current.quantityRequired*1));

        total += Number.isNaN(price) ? 0 : price;
        return total
      }, 0);
    },
  },

  methods: {
    /**
     * 进入质量要求详情页面
     * @param {string} text 质量要求文本内容
     */
    inQualityDetail(text) {
      this.$router.push({
        name: "QualityDetail",
        params: {
          id: this.id,
          msg: text,
        },
      });
    },
    // 附件上传
    afterRead() {},
    // 获取详情信息
    getDetail() {
      Toast.loading({
        duration: 0, // 持续展示 toast
        forbidClick: true,
      });
      this.id = this.$route.query.id;
      api.purchase
        .getPurchaseById({
          id: this.id,
          query: "",
          loginPhone: localStorage.phone, //电话号码
          // id: '-8919934502198097234',
          // loginPhone: '15181798337',
        })
        .then((res) => {
          Toast.clear();
          console.log('询价比 >> :',res);
          if (res.data.code == 200 && res.data.data.rows.length > 0) {
            this.list = res.data.data.rows[0].subLevelVo;
              console.log('list :>> ',this.list)
            this.msg = {
              contact: res.data.data.rows[0].contact,
              companyName: res.data.data.rows[0].companyName,
              contactPhone: res.data.data.rows[0].contactPhone,
              publisher: res.data.data.rows[0].publisher,
            };
            this.code = res.data.data.rows[0].code;
           
            for(var i=0;i< res.data.data.rows.length;i++){
              if(res.data.data.rows[i].id == this.id){
                this.whether = res.data.data.rows[i].whether;
              }
            }
            // this.whether = res.data.data.rows[0].whether;
          }
        });
    },
    /**
     * 提交报价
     */
    async submit() {
      this.loading = true;
      var res;

      if (this.file.length > 0) {
        let formData = new FormData();
        this.file.forEach((item) => {
          formData.append("file", item.file);
        });
        res = await api.file.upload(formData);
        if (res.data.code == 200) {
          this.fileName = res.data.data;
        }
      }
      var flage = true
      // 判断报价是否为空
      for (var i = 0; i < this.list.length; i++) {
        if(this.list[i].bj == ''|| this.list[i].bj == undefined||this.list[i].bj ==null&& this.list[i].jhq == ''||this.list[i].jhq ==null||this.list[i].jhq == undefined){
          flage=false
        }
      }
      if(flage){
        // if(this.whether == '是' && this.fileName == ''){
        if(false){
          Toast.fail("请上传附件后再提交");
          this.loading = false;
        }else{
          api.purchase
        .submitPurchase({
          loginPhone: localStorage.phone, //手机号
          // loginPhone: "15181798337", //手机号
          data: this.list, //报价列表
          noTaxPrice: this.allPrice, //不含税总价
          taxPrice: ((this.allPrice)*1.13).toFixed(2), //不含税总价
          accessory: this.fileName, //附件
          code: this.code, //询比价发布编码
          supplier: this.msg, //供应商信息
        })
        .then((res) => {
          this.loading = false;
          if (res.data.code == 200) {
            Toast.success("提交成功");
            setTimeout(() => {
              this.$router.push("/purchase");
            }, 800);
          } else {
            Toast.fail("提交失败");
          }
        });
        }
       
      }else{
        Toast.fail("单价或交货期不能为空");
        this.loading = false;
      }
    
    },
      // 附件预览
      upload(id){
          const toast = Toast.loading({
              duration: 0,
              forbidClick: true,
          });
          api.equipment
              .checkTheCertificateDetails(id)
              .then((res) => {
                  let { code, data, msg } = res.data || {};
                  if (code !== 200) return Toast.fail(msg);
                  if (data.indexOf("<img src=") != -1){
                      data = data.replace("<img src=","<img style='width:100%' src=")
                  }
                  this.htmlText = data
                  this.isShow = true
              })
              .finally(() => {
                  setTimeout(toast.clear, 500);
              });

      },
  },
};
</script>

<style lang="less" scoped>
.disableInput{
  border: none !important;
}
.mandatory{
  position: relative;
  &::after{
    position: absolute;
    left: -12px;
    top: 4px;
    content: '*';
    color: red;
    font-size: 20px;
  }
}
#inputNum {
  border: 1px solid #eeeeee;
  outline: none;
  border-radius: 2px;
  padding: 0 0 0 13px;
  width: 100%;
  margin: 0 10px 0 0;
}
.rows {
  /deep/ .van-cell {
    background-color: #fff !important;
    border: none !important;
  }
}
#purchaseDetails {
  width: 100vw;
  min-height: calc(100vh - 64px);
  background-color: #f5f5f5;
  padding-bottom: 132px;
  padding-top: 51px;
  box-sizing: border-box;
  position: relative;
  .nothing {
    color: #333;
    text-align: center;
    font-size: 16px;
  }
  .top {
    position: fixed;
    width: 100%;
    top: 0;
    overflow: hidden;
    background-color: white;
    z-index: 1;
  }
  & > .row {
    border-radius: 5px;
    display: flex;
    justify-content: space-between;
    padding: 0 10px;
    background-color: #fff;
    box-sizing: border-box;
    margin: 10px;
    height: auto;
    p {
      white-space: pre-wrap !important;
      font-size: 16px;
    }
  }
  .list-item {
    margin: 8px 10px;
    background: #ffffff;
    border-radius: 8px;
    padding: 12px 16px;
    overflow: hidden;
    .title {
      font-family: "Inter";
      font-style: normal;
      font-weight: 600;
      font-size: 16px;
      line-height: 19px;
      margin-bottom: 12px;
      color: #333333;
    }
    .row {
      div {
        width: 50%;
        white-space: pre-wrap !important;
      }
    }
    & > div {
      font-family: "Inter";
      font-style: normal;
      font-weight: 400;
      font-size: 14px;
      line-height: 23px;
      color: #666666;
      margin: 0;
      display: flex;
      align-items: center;
      position: relative;
      min-height: 28px;
      line-height: 24px;
      & > div:nth-of-type(1),
      & > p:nth-of-type(1) {
        white-space: nowrap;
      }
      .theUnitPrice {
        margin-right: 10px;
        flex: 2;
      }
      .inputN {
        flex: 4;
      }
      .money {
        flex: 1;
      }
      p {
        margin: 0;
      }
      .quality {
        max-width: 56%;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      .detail {
        position: absolute;
        right: 0;
        width: auto;
        background: rgba(14, 134, 209, 0.2);
        border: 1px solid #0e86d1;
        border-radius: 5px;
        font-family: "ABeeZee";
        font-style: normal;
        font-weight: 400;
        font-size: 14px;
        height: 23px;
        line-height: 23px;
        padding: 0 10px;
        text-align: center;
        letter-spacing: -0.24px;
        color: #0e86d1;
      }
    }
  }
  .msg {
    background: #ffffff;
    padding: 12px;
    .company {
      font-family: "Inter";
      font-style: normal;
      font-weight: 600;
      font-size: 16px;
      line-height: 24px;
      /* identical to box height, or 150% */

      color: #333333;
    }
    .contact {
      margin-top: 10px;
      display: flex;
      font-family: "Inter";
      font-style: normal;
      font-weight: 400;
      font-size: 14px;
      /* identical to box height, or 143% */
      color: #666666;

      div {
        width: 50%;
      }
    }
  }
  .submit {
    padding: 10px;
    width: 100%;
    position: fixed;
    bottom: 0;
    height: 127px;
    box-sizing: border-box;
    background-color: white;
    &>.row{
      border-radius: 5px;
      display: flex;
      justify-content: space-between;
      padding: 0 10px;
      background-color: #fff;
      box-sizing: border-box;
      margin: 10px;
      height: auto;
    p {
      white-space: pre-wrap !important;
      font-size: 16px;
      margin: 5px 0;
    }
    }
    /deep/ .van-button--block {
      display: flex;
      flex-direction: row;
      justify-content: center;
      align-items: center;
      padding: 10px 143.5px;
      gap: 10px;
      background: #0e86d1;
      border-radius: 4px;
    }
  }
}

/deep/ .van-ellipsis {
  overflow: visible;
}
/deep/ .van-nav-bar__left {
  padding: 10px;
}
/deep/ .van-ellipsis {
  overflow: visible;
}
/deep/ .van-nav-bar__content {
  height: 51px;
}
/deep/ .van-nav-bar__title {
  font-family: "Inter";
  font-style: normal;
  font-weight: 600;
  font-size: 18px;
  line-height: 20px;
  /* identical to box height, or 111% */

  text-align: center;
  letter-spacing: -0.24px;

  color: #333333;
}
/deep/ .van-nav-bar .van-icon {
  /* 文字颜色3 */

  color: #999999;
}
/deep/ .van-cell {
  padding: 0;
  box-sizing: border-box;
  margin-right: 10px;
  background: rgba(243, 243, 243, 0.5);
  border: 1px solid #e8e8e8;
  border-radius: 4px;
  line-height: 33px;
}
/deep/ .van-field__control {
  padding-left: 10px;
}
/deep/ .van-uploader__file-name {
  overflow: hidden;
  text-align: right;
}
/deep/ .van-uploader__input-wrapper {
  display: flex;
  align-items: center;
  justify-content: flex-end;
}
/deep/ .van-uploader__file-icon {
  display: none;
}
/deep/ .van-uploader__file {
  width: auto;
  height: auto;
  background-color: transparent;
}
/deep/ .van-field__value {
  overflow: hidden;
}
/deep/ .van-uploader__preview,
/deep/ .van-uploader,
/deep/ .van-uploader__input-wrapper {
  width: 100%;
}
/deep/ .van-uploader__preview {
  margin: 0;
}
/deep/ .van-uploader__wrapper {
  padding: 5px;
}
/deep/ .van-uploader {
  background: rgba(14, 134, 209, 0.2);
  border: 1px solid #0e86d1;
  border-radius: 5px;
  font-family: "ABeeZee";
  font-style: normal;
  font-weight: 400;
  font-size: 14px;
  line-height: 23px;
  /* identical to box height, or 143% */

  text-align: center;
  letter-spacing: -0.24px;

  color: #0e86d1;
  width: auto;
  max-width: 100%;
}
</style>
